package com.changgou.goods.service.impl;

import com.changgou.goods.dao.SkuMapper;
import com.changgou.goods.dao.SpuMapper;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.service.SpuService;
import com.changgou.goods.pojo.Spu;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SpuServiceImpl implements SpuService {

    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;

    /**
     * 添加商品第三步
     * @param spu
     */
    @Override
    public void commitSpu(Spu spu) {
        //修改spu数据
        spuMapper.updateByPrimaryKeySelective(spu);
        //批量插入sku数据
        skuMapper.batchInsert(spu.getSkuList());



    }

    /**
     * 设置主图
     * @param id
     * @param image
     */
    @Override
    public void updateImage(String id, String image) {
        Spu spu = new Spu();
        spu.setId(id);
        spu.setImage(image);
        spuMapper.updateByPrimaryKeySelective(spu);
    }


    /**
     * 商品分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Spu> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页
        PageHelper.startPage(page,size);


        //设置当前的查询条件
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //设置商品名称模糊查询
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            //设置货号名称模糊查询
            if (searchMap.get("sn") != null && !"".equals(searchMap.get("sn"))){
                criteria.andLike("sn","%"+searchMap.get("sn")+"%");
            }
            //设置品牌ID查询
            if (searchMap.get("brandId") != null){
                criteria.andEqualTo("brandId",searchMap.get("brandId"));
            }
            //设置商品类别查询
            if (searchMap.get("category1Id") != null){
                criteria.andEqualTo("category1Id",searchMap.get("category1Id"));
            }
            if (searchMap.get("category2Id") != null){
                criteria.andEqualTo("category2Id",searchMap.get("category2Id"));
            }
            if (searchMap.get("category3Id") != null){
                criteria.andEqualTo("category3Id",searchMap.get("category3Id"));
            }


        }
        Page<Spu> pageInfo = (Page<Spu>)spuMapper.selectByExample(example);
        return pageInfo;
    }

    @Override
    public List<Map<String,Object>> findAll() {
        int a = 1;  //成功状态码
        int b = 0;  //失败状态码
        int  state = spuMapper.findState(a);   //查询上架的数据
        int NoState = spuMapper.findState(b);     //查询未上架的数据


        int findAll = spuMapper.findAll();  //查询总共的数据
        List<Map<String,Object>> all = new ArrayList<>();
        Map map= new HashMap();
        Map map1= new HashMap();
        Map map2= new HashMap();

        map.put("conut",findAll);
        map.put("name","全部商品");
        map1.put("conut",state);
        map1.put("name","上架商品");
        map2.put("conut",NoState);
        map2.put("name","未上架商品");

        all.add(map);
        all.add(map1);
        all.add(map2);

        return all;
    }

}
